<?php
/**
 * 好友邀请类
 * @author HuangJie
 * @create_date 2008-02-25
 */

class FriendRequest{  
    
    /**
     * 添加好友邀请记录
     *
     * @param $conn 数据库连接类
     * @param : array $arr_input - baby_id ,requrest_type ,baby_fid ,friend_name ,comment
     */
    private function addFriendRequest($conn,$arr_input){
        $str_query = "insert into";
        $str_query .= " baby_friend_request";
        $str_query .= " set";
        $str_query .= " baby_id={$arr_input['baby_id']}";
        $str_query .= ", baby_fid={$arr_input['baby_fid']}";
        $str_query .= ", requrest_type={$arr_input['requrest_type']}";
        $str_query .= ", friend_name='{$arr_input['friend_name']}'";
        $str_query .= ", comment='{$arr_input['comment']}'";
        $str_query .= ", request_date='".$conn->getCurrentTime()."'";
        
        $conn->executeQuery($str_query);
    }
    
    /**
     * 删除好友请求
     *
     * @param $conn 数据库连接类
     * @param : array $arr_input - baby_id ,requrest_type ,baby_fid
     */
    public function deleteFriendRequest($conn,$arr_input){
        $str_query = "delete from";
        $str_query .= " baby_friend_request";
        $str_query .= " where";
        $str_query .= " baby_id={$arr_input['baby_id']}";
        $str_query .= "and baby_fid={$arr_input['baby_fid']}";
        $str_query .= "and requrest_type={$arr_input['requrest_type']}";
        
        $conn->executeQuery($str_query);
    }
    
    /**
     * 邀请结为好友
     *
     * @param $conn 数据库连接类
     * @param : array $arr_input - baby_id ,baby_name,requrest_type ,baby_fid ,friend_name ,comment ,request_date
     */
    public function inviteFriend($conn,$arr_input){
        /* 添加一条邀请别人的记录 */
        unset($arr_tmp_input);
        $arr_tmp_input['baby_id'] = $arr_input['baby_id'];
        $arr_tmp_input['baby_fid'] = $arr_input['baby_fid'];
        $arr_tmp_input['friend_name'] = $arr_input['friend_name'];
        $arr_tmp_input['requrest_type'] = 1;
        $arr_tmp_input['comment'] = $arr_input['comment'];
        $this->addFriendRequest($conn,$arr_tmp_input);
        
        /* 添加对方一个被邀请的记录 */
        unset($arr_tmp_input);
        $arr_tmp_input['baby_id'] = $arr_input['baby_fid'];
        $arr_tmp_input['baby_fid'] = $arr_input['baby_id'];
        $arr_tmp_input['friend_name'] = $arr_input['baby_name'];
        $arr_tmp_input['requrest_type'] = 2;
        $arr_tmp_input['comment'] = $arr_input['comment'];
        $this->addFriendRequest($conn,$arr_tmp_input);
    }
    
    /**
     * 获取好友邀请列表
     *
     * @param $conn 数据库连接类
     * @param : array $arr_input - baby_id ,requrest_type ,baby_fid ,friend_name ,comment ,request_date
     * @param string $limit 
    * @return : $arr_output
    *			baby_id, 			[0]<br>
    *			requrest_type, 		[1]<br>
    *			baby_fid, 			[2]<br>
    *			friend_name, 		[3]<br>
    *			comment, 			[4]<br>
    *			request_date, 		[5]<br>
     */
    public function getFriendRequestList($conn,$arr_input,$limit = null){
        $str_query = "select";
        if (isset($arr_input['cnt'])) {
            $str_query .= " count(1)";
        }else{
            $str_query .= "  baby_id";
            $str_query .= ", baby_fid";
            $str_query .= ", friend_name";
            $str_query .= ", requrest_type";
            $str_query .= ", comment";
            $str_query .= ", request_date";
        }
        $str_query .= " from";
        $str_query .= " baby_friend_request";
        $str_query .= " where baby_id={$arr_input['baby_id']}";
        $str_query .= " and requrest_type={$arr_input['requrest_type']}";
        if (isset($arr_input['baby_fid'])) {
        	$str_query .= " and baby_fid={$arr_input['baby_fid']}";
        }

        if (isset($arr_input['orderby'])) {
        	$str_query .= " order by {$arr_input['orderby']}";
        }

        if ($limit != null){
        	$str_query .= " {$limit}";
        }

        $arr_output = $conn->selectQuery($sql);
        return $arr_output;
    }
}
?>